/**
 * 
 */
package com.gmail.blackdog1987.carcare.activity;

import in.srain.cube.image.CubeImageView;
import in.srain.cube.image.ImageLoader;
import in.srain.cube.image.ImageLoaderFactory;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.gmail.blackdog1987.carcare.App;
import com.gmail.blackdog1987.carcare.BaseActivity;
import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.entity.TCarRent;
import com.gmail.blackdog1987.carcare.util.ModelUtil;
import com.gmail.blackdog1987.carcare.widget.ImgViewerWindow;

@EActivity(R.layout.activity_carrent_detail)
public class CarRentDetailActivity extends BaseActivity implements OnClickListener {
	private Context mContext;
	private TCarRent mCar;

	// views
	@ViewById(R.id.carrent_detail_title)
	public TextView mTitle;
	@ViewById(R.id.carrent_detail_head)
	public CubeImageView mHead;
	@ViewById(R.id.carrent_detail_info)
	public TextView mInfo;
	@ViewById(R.id.carrent_detail_price)
	public TextView mPrice;
	@ViewById(R.id.carrent_detail_img1)
	public CubeImageView mImg1;
	@ViewById(R.id.carrent_detail_img2)
	public CubeImageView mImg2;
	@ViewById(R.id.carrent_detail_img3)
	public CubeImageView mImg3;
	@ViewById(R.id.carrent_detail_img4)
	public CubeImageView mImg4;
	@ViewById(R.id.carrent_detail_img5)
	public CubeImageView mImg5;
	@ViewById(R.id.carrent_detail_color)
	public TextView mColor;
	@ViewById(R.id.carrent_detail_capacity)
	public TextView mCapacity;
	@ViewById(R.id.carrent_detail_size)
	public TextView mSize;
	@ViewById(R.id.carrent_detail_date)
	public TextView mDate;
	@ViewById(R.id.carrent_detail_engine)
	public TextView mEngine;
	@ViewById(R.id.carrent_detail_drivetype)
	public TextView mDriveType;
	@ViewById(R.id.carrent_detail_transmission)
	public TextView mTransmission;
	@ViewById(R.id.carrent_detail_gastype)
	public TextView mGastype;
	@ViewById(R.id.carrent_detail_carlevel)
	public TextView mCarlevel;
	@ViewById(R.id.carrent_detail_tail)
	public TextView mPressure;
	@ViewById(R.id.carrent_detail_abs)
	public TextView mABS;
	@ViewById(R.id.carrent_detail_esp)
	public TextView mESP;
	@ViewById(R.id.carrent_detail_childseat)
	public TextView mChildSeat;
	@ViewById(R.id.carrent_detail_remotekey)
	public TextView mRemoteKey;
	@ViewById(R.id.carrent_detail_cruise)
	public TextView mCruise;
	@ViewById(R.id.carrent_detail_powersunroof)
	public TextView mPowerSunroof;
	@ViewById(R.id.carrent_detail_oxlight)
	public TextView mXenon;
	@ViewById(R.id.carrent_detail_shop)
	public TextView mShop;
	@ViewById(R.id.carrent_detail_phone)
	public TextView mPhone;
	
	private Intent imgViewIntent;

	@AfterViews
	public void afterViews() {
		mContext = this;
		initData();
		reg();
	}

	public void reg() {
		findViewById(R.id.back).setOnClickListener(this);
	}

	public void initData() {
		imgViewIntent = new Intent(CarRentDetailActivity.this, ImgViewerWindow.class);
		mCar = (TCarRent) getIntent().getSerializableExtra("data");

		// set views data
		ImageLoader imageLoader = ImageLoaderFactory.create(mContext);
		if (mCar.headurl != null && mCar.headurl.length() != 0) {
			App.loadImg(mHead, imageLoader, mCar.headurl);
		}
		if (mCar.img1 != null && mCar.img1.length() != 0) {
			App.loadImg(mImg1, imageLoader, mCar.img1);
			mImg1.setOnClickListener(this);
		}
		if (mCar.img2 != null && mCar.img2.length() != 0) {
			App.loadImg(mImg2, imageLoader, mCar.img2);
			mImg2.setOnClickListener(this);
		}
		if (mCar.img3 != null && mCar.img3.length() != 0) {
			App.loadImg(mImg3, imageLoader, mCar.img3);
			mImg3.setOnClickListener(this);
		}
		if (mCar.img4 != null && mCar.img4.length() != 0) {
			App.loadImg(mImg4, imageLoader, mCar.img4);
			mImg4.setOnClickListener(this);
		}
		if (mCar.img5 != null && mCar.img5.length() != 0) {
			App.loadImg(mImg5, imageLoader, mCar.img5);
			mImg5.setOnClickListener(this);
		}

		mTitle.setText(ModelUtil.getCarrentTitle(mCar));
		mInfo.setText(ModelUtil.getCarrentInfo(mCar));
		mPrice.setText(String.valueOf(mCar.getPrice()));
		mColor.setText(mCar.getColor());
		mCapacity.setText(mCar.getCapacity());
		mSize.setText(mCar.getSize());
		mDate.setText(new SimpleDateFormat("yyyy-MM-dd").format(new Date(mCar.getFactorytime())));
		mEngine.setText(mCar.getEngine());
		mDriveType.setText(mCar.getDriveType());
		mTransmission.setText(mCar.getTransmission());
		mGastype.setText(mCar.getGasType());
		mCarlevel.setText(String.valueOf(mCar.getCarLevel()));
		mShop.setText(mCar.getShop());
		mPhone.setText(mCar.getPhone());
		// 亮点功能，需要修改背景
		if (mCar.getPressure() != null && 1 == mCar.getPressure().intValue()) {
			mPressure.setBackgroundResource(R.color.green);
		}
		if (mCar.getAbs() != null && 1 == mCar.getAbs().intValue()) {
			mABS.setBackgroundResource(R.color.green);
		}
		if (mCar.getEsp() != null && 1 == mCar.getEsp().intValue()) {
			mESP.setBackgroundResource(R.color.green);
		}
		if (mCar.getChildSeat() != null && 1 == mCar.getChildSeat().intValue()) {
			mChildSeat.setBackgroundResource(R.color.green);
		}
		if (mCar.getRemoteKey() != null && 1 == mCar.getRemoteKey().intValue()) {
			mRemoteKey.setBackgroundResource(R.color.green);
		}
		if (mCar.getCruise() != null && 1 == mCar.getCruise().intValue()) {
			mCruise.setBackgroundResource(R.color.green);
		}
		if (mCar.getPowerSunroof() != null && 1 == mCar.getPowerSunroof().intValue()) {
			mPowerSunroof.setBackgroundResource(R.color.green);
		}
		if (mCar.getXenon() != null && 1 == mCar.getXenon().intValue()) {
			mXenon.setBackgroundResource(R.color.green);
		}
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		default:
			break;
		case R.id.carrent_detail_img1:
			imgViewIntent.putExtra("url", mCar.img1);
			startActivity(imgViewIntent);
			break;
		case R.id.carrent_detail_img2:
			imgViewIntent.putExtra("url", mCar.img2);
			startActivity(imgViewIntent);
			break;
		case R.id.carrent_detail_img3:
			imgViewIntent.putExtra("url", mCar.img3);
			startActivity(imgViewIntent);
			break;
		case R.id.carrent_detail_img4:
			imgViewIntent.putExtra("url", mCar.img4);
			startActivity(imgViewIntent);
			break;
		case R.id.carrent_detail_img5:
			imgViewIntent.putExtra("url", mCar.img5);
			startActivity(imgViewIntent);
			break;
		}

	}

	// click area
	@Click(R.id.back)
	void back() {
		finish();
	}

	@Click(R.id.carrent_detail_pay)
	void pay() {
		Intent i = new Intent(this, OrderCarRentActivity_.class);
		i.putExtra("data", mCar);
		startActivity(i);
	}
}
